package com.itheima.controller.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.ums.UmsMenu;
import com.itheima.pojo.ums.vo.UmsMenuVo;
import com.itheima.service.ums.UmsMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @DubboReference
    private UmsMenuService umsMenuService;

    /**
     * 添加后台菜单
     * @param umsMenu
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody UmsMenu umsMenu){
        umsMenuService.save(umsMenu);
        return R.success("success");
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id){
        umsMenuService.removeById(id);
        return R.success();
    }


    /**
     * 分页查询后台菜单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{parentId}")
    public R list(@PathVariable("parentId") Integer parentId,  Integer pageNum, Integer pageSize) {
        PageInfo page = umsMenuService.selectList(parentId,pageNum,pageSize);
        return R.success(page);

    }

    /**
     * 修改后台菜单
     * @param umsMenu
     * @return
     */
  @PostMapping("/update/{id}")
    public R update(@RequestBody UmsMenu umsMenu){
      umsMenuService.updateMenu(umsMenu);
      return R.success("success");
  }

    /**
     * 修改菜单显示状态
     * @param id
     * @param hidden
     * @return
     */
  @PostMapping("/updateHidden/{id}")
    public R updateHidden(@PathVariable("id") Integer id,Integer hidden){
      umsMenuService.updateHidden(id,hidden);
      return R.success("success");
  }

    /**
     * 根据Id获取菜单详情
     * @param id
     * @return
     */
  @GetMapping("/{id}")
    public R menu(@PathVariable Integer id){
     UmsMenu umsMenu= umsMenuService.selectMenu(id);
     return R.success(umsMenu);
  }

    /**
     * 树形结构返回所有菜单列表
     * @return
     */
  @GetMapping("/treeList")
    public R treeList(){
      List<UmsMenuVo> umsMenuVoList = umsMenuService.treeList();
      return R.success(umsMenuVoList);
  }
}
